package fudan.se.project.domain.sysrole;

import com.fasterxml.jackson.annotation.JsonIgnore;
import fudan.se.project.domain.User;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Gao Longchao
 */
@Entity
@Setter
@Getter
@NoArgsConstructor
public class SystemAuthority implements GrantedAuthority {

    private static final long serialVersionUID = -8974777274465208640L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Enumerated(EnumType.STRING)
    @Column(unique = true)
    private Authority authority;

    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "systemAuthorities")
    private Set<User> users = new HashSet<>();


    public SystemAuthority(Authority authority) {
        this.authority = authority;
    }

    @Override
    public String getAuthority() {
        return authority.toString();
    }


    public enum Authority {
        USER,
        ADMIN
    }
}
